package com.csw.shuanfa.bitset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class ShuffListMethodSortBoolean {

    public static int num = 10000;

    public static void main(String[] args) {
        List<String> cardList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            cardList.add("cardId" + i);
        }

        long startTime = System.currentTimeMillis();

        List<String> randomList = shuffleMethod(cardList);

        long endTime = System.currentTimeMillis();
        //花费时间:" + (endTime - startTime));
//        //randomList);
    }

    /**
     * 随机拿到配置num数量的数据
     */
    private static List<String> shuffleMethod(List<String> resultList) {
        //防止列表长度比需要的数量短,按照长度短的决定最终数量
        int randomNum = Math.min(num, resultList.size());

        Random random = new Random();
        // 索引列表
        List<Integer> indexes = new ArrayList<>();
        while (indexes.size() < randomNum) {
            int index = random.nextInt(resultList.size());
            if (!indexes.contains(index)) {
                indexes.add(index);
            }
        }

        //对indexes排序
        Collections.sort(indexes);
        //取出indexes对应的list放到newList
        List<String> randomList = new ArrayList<>();
        for (int index : indexes) {
            randomList.add(resultList.get(index));
        }

        return randomList;
    }
}

